package com.zhongshengnetwork.rightbe.common.pulluprefresh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhongshengnetwork.rightbe.R;

import java.util.List;

public class Listadapter extends BaseAdapter {
	//上下文
    private Context mContext;
    //item 中绑的数据
    private List<String> listBeans;
    //构造方法
    public Listadapter(Context context, List<String> listBeans) {
        this.mContext = context;
        this.listBeans = listBeans;
    }

    @Override
    public int getCount() {
        return listBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //ViewHolder
        ViewHolder viewHolder=null;
        //绘制每一项的时候先判断convertView是否为空，不为空，则else里面去复用，为空，则重新赋予item布局
        if (convertView == null) {
            //new出ViewHolder ，初始化布局文件
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_item, null);
            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
            //调用convertView的setTag方法，将viewHolder放入进去，用于下次复用
            convertView.setTag(viewHolder);
        } else {
            //复用已经存在的item的项
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.content.setText(listBeans.get(position));
        return convertView;
    }

    class ViewHolder {
        TextView content;

    }
}
